/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package economicocontrol.Pantallas;

import economicocontrol.Constante.Constante;
import economicocontrol.bl.SeguridadBL;
import economicocontrol.dao.entity.RegistroDepartamentos;
import economicocontrol.dao.entity.RegistroEconomicos;
import economicocontrol.dao.entity.RegistroPersonal;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author Bere
 */
public class Seguridad extends javax.swing.JFrame {
    private int van;

    /**
     * Creates new form Seguridad
     */
    public Seguridad() {
        segBl=new SeguridadBL();
        segBl.regDepAll();
        
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        numEcoText = new javax.swing.JTextField();
        fecSalText = new javax.swing.JTextField();
        fecEntText = new javax.swing.JTextField();
        horSalText = new javax.swing.JTextField();
        horEntText = new javax.swing.JTextField();
        kmSalText = new javax.swing.JTextField();
        kmEntText = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        depText = new javax.swing.JTextField();
        PerCombo = new javax.swing.JComboBox();
        jButton2 = new javax.swing.JButton();

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("SEGURIDAD");

        jLabel1.setText("No. Economico");

        jLabel2.setText("Autiriza");

        jLabel3.setText("Fecha Entrada");

        jLabel4.setText("Fecha Salida");

        jLabel5.setText("Hora Entrada");

        jLabel6.setText("Hora Salida");

        jLabel7.setText("Km Entrada");

        jLabel8.setText("Km Salida");

        numEcoText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                numEcoTextKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                numEcoTextKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                numEcoTextKeyTyped(evt);
            }
        });

        fecSalText.setEditable(false);
        fecSalText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fecSalTextActionPerformed(evt);
            }
        });

        fecEntText.setEditable(false);

        horSalText.setEditable(false);
        horSalText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                horSalTextActionPerformed(evt);
            }
        });

        horEntText.setEditable(false);

        kmSalText.setEditable(false);

        kmEntText.setEditable(false);

        jLabel9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/cfe.jpg"))); // NOI18N

        jButton1.setText("Agregar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel10.setText("Departamento");

        depText.setEditable(false);
        depText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                depTextActionPerformed(evt);
            }
        });

        PerCombo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                PerComboMouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                PerComboMouseExited(evt);
            }
        });
        PerCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PerComboActionPerformed(evt);
            }
        });

        jButton2.setText("buscar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(119, 119, 119)
                        .addComponent(jButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(66, 66, 66)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel9)
                                .addGap(127, 127, 127))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel8)
                                    .addComponent(jLabel10))
                                .addGap(30, 30, 30)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(fecSalText)
                                    .addComponent(fecEntText)
                                    .addComponent(horSalText)
                                    .addComponent(horEntText)
                                    .addComponent(kmSalText)
                                    .addComponent(kmEntText, javax.swing.GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE)
                                    .addComponent(numEcoText)
                                    .addComponent(depText)
                                    .addComponent(PerCombo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                        .addComponent(jButton2)))
                .addContainerGap(32, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jLabel9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(numEcoText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton2)))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(PerCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jLabel10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(depText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(fecSalText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(fecEntText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(horSalText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(horEntText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(kmSalText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(kmEntText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap())
        );

        fecSalText.setText(segBl.getDate().getDay()+"/"+segBl.getDate().getMonth()+"/"+segBl.getDate().getYear());
        horSalText.setText(segBl.getDate().getHours()+":"+segBl.getDate().getMinutes()+":"+segBl.getDate().getSeconds());
        segBl.regPerAll();         for (Iterator<RegistroPersonal> iterator = segBl.getListEntPer().iterator(); iterator.hasNext();) {             RegistroPersonal next = iterator.next();             PerCombo.addItem(next.getNombrePersonal()+" "+next.getApellidoPaterno()+" "+next.getApellidoMaterno());         }

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        //segBl.regEcoSalXEco(Integer.parseInt(numEcoText.getText()));
        if (van==1) {
            
            if(segBl.insertSeguridad(kmSalText.getText())){
                this.cleanCamp();
                numEcoText.setText("");   
            }else
                JOptionPane.showMessageDialog(PerCombo,Constante.MESAJE_COMPARAR_SAL_ENT_KM+segBl.getRegEntSalDAO().listKmEntrada(Integer.parseInt(numEcoText.getText().trim())));
            
        }else if (van == 2) {
            
            if(segBl.updateSeguridad(kmEntText.getText())){
                this.cleanCamp();
                numEcoText.setText("");
            }else
                JOptionPane.showMessageDialog(PerCombo,Constante.MESAJE_COMPARAR_ENT_SAL_KM);
        }
       
    }//GEN-LAST:event_jButton1ActionPerformed

    private void fecSalTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fecSalTextActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_fecSalTextActionPerformed

    private void horSalTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_horSalTextActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_horSalTextActionPerformed

    private void numEcoTextKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_numEcoTextKeyPressed
        // TODO add your handling code here:        
    }//GEN-LAST:event_numEcoTextKeyPressed

    private void numEcoTextKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_numEcoTextKeyReleased
        // TODO add your handling code here:
        //System.out.println("jTextField1KeyReleased");
        //segBl.regEcoBus(Integer.parseInt(jTextField1.getText()));
        
    }//GEN-LAST:event_numEcoTextKeyReleased

    private void numEcoTextKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_numEcoTextKeyTyped
        // TODO add your handling code here:
        //System.out.println("jTextField1KeyTyped");
    }//GEN-LAST:event_numEcoTextKeyTyped

    private void depTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_depTextActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_depTextActionPerformed

    private void PerComboMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_PerComboMouseClicked
        // TODO add your handling code here:
        segBl.regPerUbi(PerCombo.getSelectedIndex());
        depText.setText(segBl.getEntPer().getRegistroDepartamentosIdRegistroDepartamento().getNombreDepartamento());
    
    }//GEN-LAST:event_PerComboMouseClicked

    private void PerComboMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_PerComboMouseExited
        // TODO add your handling code here:
    
    }//GEN-LAST:event_PerComboMouseExited

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        segBl.resDate();
        //if(segBl.regEcoSalXEco(numEcoText.getText().trim())){
            this.cleanCamp();
            if(segBl.regEcoSalXEco(numEcoText.getText().trim())){
            //PerCombo.removeAllItems();
                kmEntText.setEditable(true);
                kmSalText.setEditable(false);
                kmEntText.setText("");
                segBl.resDate();
                PerCombo.addItem(segBl.getEntEntSal().getRegistroPersonalidRegistroPersonal().getNombrePersonal()+" "+
                                  segBl.getEntEntSal().getRegistroPersonalidRegistroPersonal().getApellidoPaterno()+" "+
                                  segBl.getEntEntSal().getRegistroPersonalidRegistroPersonal().getApellidoMaterno());
           
               depText.setText(segBl.getEntEntSal().getRegistroDepartamentosIdRegistroDepartamento().getNombreDepartamento());
               fecSalText.setText(segBl.getEntEntSal().getFechaSalida().getDay()+"/"+
                               segBl.getEntEntSal().getFechaSalida().getMonth()+"/"+
                               segBl.getEntEntSal().getFechaSalida().getYear());
           
               fecEntText.setText(segBl.getDate().getDay()+"/"+
                                  segBl.getDate().getMonth()+"/"+
                                  segBl.getDate().getYear());
           
                horSalText.setText(segBl.getEntEntSal().getHorasalida().getHours()+":"+
                                   segBl.getEntEntSal().getHorasalida().getMinutes()+":"+
                                   segBl.getEntEntSal().getHorasalida().getSeconds());
           
                horEntText.setText(segBl.getDate().getHours()+":"+
                                  segBl.getDate().getMinutes()+":"+
                                  segBl.getDate().getSeconds());
           
                kmSalText.setText(segBl.getEntEntSal().getKilometrajeSalida().toString());
                van = 2;
           
            }else{
                
                if(segBl.regEcoBus(numEcoText.getText().trim())){
                    kmEntText.setEditable(false);
                    kmSalText.setEditable(true);
                    kmSalText.setText("");
                    if(segBl.getListEntEco().size()==1){
                    depText.setText(segBl.getEntEco().getRegistroPersonalidRegistroPersonal().getRegistroDepartamentosIdRegistroDepartamento().getNombreDepartamento());
                    fecSalText.setText(segBl.getDate().getDay()+"/"+
                                  segBl.getDate().getMonth()+"/"+
                                  segBl.getDate().getYear());
                    horSalText.setText(segBl.getDate().getHours()+":"+
                                  segBl.getDate().getMinutes()+":"+
                                  segBl.getDate().getSeconds());
                }else{
                    numEcoText.setText("");
                }
                van = 1;
                }else{
                    JOptionPane.showMessageDialog(PerCombo,Constante.MESAJE_NUM_ECONOMICO_NO_EXISTE);
                }
            }        
    }//GEN-LAST:event_jButton2ActionPerformed

    private void PerComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PerComboActionPerformed
        // TODO add your handling code here:
        
    }//GEN-LAST:event_PerComboActionPerformed

    private void cleanCamp(){
        
        //numEcoText.setText("");
        //PerCombo.removeAllItems();
        depText.setText("");
        fecEntText.setText("");
        fecSalText.setText("");
        horEntText.setText("");
        horSalText.setText("");
        kmSalText.setText("");
        kmEntText.setText("");
        
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Seguridad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Seguridad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Seguridad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Seguridad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Seguridad().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox PerCombo;
    private javax.swing.JTextField depText;
    private javax.swing.JTextField fecEntText;
    private javax.swing.JTextField fecSalText;
    private javax.swing.JTextField horEntText;
    private javax.swing.JTextField horSalText;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField kmEntText;
    private javax.swing.JTextField kmSalText;
    private javax.swing.JTextField numEcoText;
    // End of variables declaration//GEN-END:variables
    private SeguridadBL segBl;
    
}
